package com.chiaching.ums.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chiaching.common.utils.PageUtils;
import com.chiaching.common.utils.Query;
import com.chiaching.framwork.domian.bean.SuperMallUserDetail;
import com.chiaching.ums.dao.UmsMemberDao;
import com.chiaching.ums.entity.UmsMemberEntity;
import com.chiaching.ums.service.UmsMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Map;
import java.util.Objects;


@Service("umsMemberService")
@Slf4j
public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberDao, UmsMemberEntity> implements UmsMemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UmsMemberEntity> page = this.page(
                new Query<UmsMemberEntity>().getPage(params),
                new QueryWrapper<UmsMemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SuperMallUserDetail queryByIdToFramwork(Long userId) {
        UmsMemberEntity umsMemberEntity = getById(userId);
        if (Objects.isNull(umsMemberEntity)){
            return null;
        }
        SuperMallUserDetail superMallUserDetail = new SuperMallUserDetail();
        BeanUtil.copyProperties(umsMemberEntity, superMallUserDetail, true);
        log.info("security框架请求 param[userId:{}] res[{}]",userId,superMallUserDetail);
        return superMallUserDetail;
    }

    @Override
    public UmsMemberEntity queryById(Long userId) {
        return getById(userId);
    }

    @Override
    public SuperMallUserDetail queryByUsername(String username) {
        QueryWrapper<UmsMemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        UmsMemberEntity umsMemberEntity = getOne(queryWrapper, true);
        SuperMallUserDetail superMallUserDetail = new SuperMallUserDetail();
        BeanUtil.copyProperties(umsMemberEntity, superMallUserDetail, true);
        return superMallUserDetail;
    }

}